import NProgress from "@/utils/nprogress";
import router from "./index";
import { useRouters } from "@/hooks/useRouter";

router.beforeEach(async (to, from, next) => {
  const { dynamicList } = useRouters();

  const token = localStorage.getItem(localStorage.getItem("tokenName"))

  NProgress.start();

  if (from.path === "/login") {
    dynamicList.forEach(item => {
      router.addRoute(item);
    })
  }

  if (to.path === "/") {
    if (token) {
      return next("/home");
    } else {
      return next("/login");
    }
  }

  next()
});

router.afterEach(() => {
  NProgress.done();
});
